package com.zgroup.domain

import com.zgroup.common.CheckUtils
import com.zgroup.domain.SysUser

/**
 * 信息发布
 */
class InfoRelease {
    def infoReleaseService
    final static int MAX_UPLOAD_SIZE = 4            //最大上传文件数 4件
    final static int MAX_UPLOAD_BYTES = 1048576     //最大上传字节数 1M
    final static int SHORT_DESC_CHARS = 20
    final static String UPLOAD_FILE_PATH = "/upload/info_picture/"     //发布信息图片上传路径
    final static String UPLOAD_THUMBNAIL_PATH = "/upload/info_thumbnail/"     //发布信息缩略图上传路径
    final static String DEFAULT_COVER_PIC = "/images/default/upload_info_pic.jpg"
    final static String COOKIE_INFO_HISTORY = "COOKIE_INFO_HISTORY"
    final static int MAX_INFO_HISTORY = 3
    public final static String INFO_TYPE_RENT="10"
    public final static String INFO_TYPE_SALE="11"

    String code//自主编号
    Long fromEstateId
    String infoType   // 10:房源出租 11:房源出售
    String title
    String keywords
    String keywordsCode
    String price
    String unit="元/月"
    String description  //相关说明
    String contact
    String telephone
    String mobile
    String qq
    String msn

    String fromEstateType
    String fromEstateArea
    String fromEstateSector;

    String showFlag = '1'  // 显示标记位 1：显示 0：不显示
    String approveFlag = '1'  // 审批通过标记 0:审核中 1：通过  2:不通过

    InfoPicture coverPic    // 设定为形象的图片文件
    Integer clicks = 0    // 点击次数
    Integer estatesCount = 0    // 指定到的楼盘数
    Integer picsCount = 0


    String approveOption

    Date dateCreated
    Date lastUpdated
    User publisher
    SysUser approveUser
    static belongsTo = Estate
    static hasMany = [estates: Estate, pictures: InfoPicture]
    //static hasOne = [publisher:User]

    static transients = ['shortDesc', 'keyword', 'picSize', 'fullCoverPic', 'thumbCoverPic', 'fullPicFolder', 'thumbPicFolder','fromEstate']
                        
    static constraints = {
        code(nullable: true, maxSize: 10)
        title(blank: false, maxSize: 30)
        infoType(blank: false, maxSize: 6)
        keywords(nullable: true,maxSize: 255)
        keywordsCode(nullable: true,maxSize: 255)
        price(nullable: true,maxSize: 11)
        unit(nullable: true,maxSize: 10)
        description(blank: false)
        contact(blank: false, maxSize: 20)
        mobile(matches: "[0-9]+", maxSize: 30)
        telephone(maxSize: 30)
        msn(nullable: true, email: true, maxSize: 50)
        qq(nullable: true, matches: "[0-9]+", maxSize: 20)
        coverPic(nullable: true)
        approveUser(nullable: true)
        fromEstateId(nullable: true)
        fromEstateType(nullable: true)
        fromEstateArea(nullable: true)
        fromEstateSector(nullable: true)
        showFlag(blank: false, maxSize: 6)
        approveFlag(blank: false, maxSize: 6)
      approveOption(nullable: true)
    }
    def beforeInsert = {
        def bo=getFromEstate()
        fromEstateType =bo.type
        fromEstateArea =bo.area
        fromEstateSector =bo.sector
    }
    static mapping = {
        cache true
        description type: "text"
        dynamicInsert true
        dynamicUpdate true
    }

    def countEstates(){
      estatesCount=infoReleaseService.countPublishedEstate(this)
    }
    Estate getFromEstate(){
      if(fromEstateId){
         Estate.get(fromEstateId)
      }
    }

    String getShortDesc() {
        if (!CheckUtils.isEmpty(description) && description.length() > SHORT_DESC_CHARS) {
            return description.substring(0, SHORT_DESC_CHARS)
        }
        return description
    }

    String getKeyword() {
        keywords?.split(",")[0]
    }

    int getPicSize() {
        if (pictures == null) {
            return 0
        }
        return pictures.size()
    }

    String getFullCoverPic() {
        if (coverPic == null) {
            return DEFAULT_COVER_PIC
        }
        return coverPic.fileFullName
    }

    String getThumbCoverPic() {
        if (coverPic == null) {
            return DEFAULT_COVER_PIC
        }
        return coverPic.thumbFullName
    }

    String getFullPicFolder() {
        return UPLOAD_FILE_PATH + id + "/"
    }

    String getThumbPicFolder() {
        return UPLOAD_THUMBNAIL_PATH + id + "/"
    }

    String toString() {
        "id:" + id + ",title:" + title + ",picSize:" + picSize+",publisher:"+ publisher
    }
}

